package com.online.service_edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.online.service_edu.entity.EduChapter;
import com.online.service_edu.entity.EduCourse;
import com.online.service_edu.entity.EduCourseDescription;
import com.online.service_edu.entity.vo.CourseInfo;
import com.online.service_edu.mapper.EduCourseMapper;
import com.online.service_edu.service.EduChapterService;
import com.online.service_edu.service.EduCourseDescriptionService;
import com.online.service_edu.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.online.service_edu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author hdc2008
 * @since 2021-02-11
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public String addCourseInfo(CourseInfo courseInfo) {
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfo,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert>0){
            EduCourseDescription eduCourseDescription = new EduCourseDescription();
            String id = eduCourse.getId();
            BeanUtils.copyProperties(courseInfo,eduCourseDescription);
            eduCourseDescription.setId(id);
            eduCourseDescriptionService.save(eduCourseDescription);
        }
        return eduCourse.getId();
    }

    @Override
    public CourseInfo getCourseInfo(String id) {
        CourseInfo courseInfo=new CourseInfo();
        EduCourse eduCourse = baseMapper.selectById(id);
        BeanUtils.copyProperties(eduCourse,courseInfo);
        EduCourseDescription description = eduCourseDescriptionService.getById(id);
        courseInfo.setDescription(description.getDescription());
        return courseInfo;


    }

    @Override
    public void updateCourseInfo(CourseInfo courseInfo) {
        EduCourse eduCourse=new EduCourse();
        BeanUtils.copyProperties(courseInfo,eduCourse);
        int insert = baseMapper.updateById(eduCourse);
        if(insert>0){
            EduCourseDescription eduCourseDescription = new EduCourseDescription();
            BeanUtils.copyProperties(courseInfo,eduCourseDescription);
            eduCourseDescriptionService.updateById(eduCourseDescription);
        }
    }

    @Override
    public boolean removeCourseById(String id) {
        QueryWrapper<EduChapter> eduChapterWrapper=new QueryWrapper<>();
        eduChapterWrapper.eq("course_id",id);
        List<EduChapter> eduChapterList = eduChapterService.list(eduChapterWrapper);
        for (EduChapter eduChapter : eduChapterList) {


        }
        return false;
    }
}
